package com.exhibition.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.exhibition.service.ITbExhUnitService;
import com.exhibition.service.ITbInviteService;
import com.exhibition.service.ITbUnitService;
import com.exhibition.util.Util;

@Controller
public class TbInviteController {
	
	@Autowired
	ITbInviteService tbInviteService;
	@Autowired
	ITbUnitService tbUnitService;
	@Autowired
	ITbExhUnitService tbExhUnitService;
	
	
	@RequestMapping("host/addInvite.do")	
	public ModelAndView addInvite(HttpServletRequest request,String ehi_id){
		ModelAndView mav=new ModelAndView("./exhinfo/addInvite.jsp");
		//查询所有单位信息
		Map<String,Object> map=new HashMap<String,Object>();
		mav.addObject("list", tbUnitService.getList(map));
		mav.addObject("exh_id", ehi_id);
		return mav;
	}
	@RequestMapping("host/addInviteOk.do")	
	public ModelAndView addInviteOk(HttpServletRequest request,String exh_id,String[] unit_id,String inv_content){
		ModelAndView mav=new ModelAndView("redirect:/host/exhinfoList.do");
		for (int i = 0; i < unit_id.length; i++) {
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("inv_id", Util.getPrimaryKey());
			map.put("exh_id", exh_id);
			map.put("unit_id", unit_id[i]);
			map.put("inv_content", inv_content);
			tbInviteService.add(map);
		}
		return mav;
	}
	@RequestMapping("host/inviteCountList.do")	
	public ModelAndView inviteCountList(HttpServletRequest request,String exh_id){
		ModelAndView mav=new ModelAndView("./exhinfo/inviteList.jsp");
		//查询所有发出邀请函的单位
		Map<String,Object> invMap=new HashMap<String,Object>();
		invMap.put("exh_id", exh_id);
		mav.addObject("list", tbInviteService.getList(invMap));
		return mav;
	}
	@RequestMapping("unit/inviteList.do")	
	public ModelAndView inviteList(HttpServletRequest request,String ehi_name){
		ModelAndView mav=new ModelAndView("./invite/list.jsp");
		//查询所有发出邀请函的单位
		Map<String,Object> invMap=new HashMap<String,Object>();
		invMap.put("unit_id", request.getSession().getAttribute("unit_id"));
		invMap.put("ehi_name", ehi_name);
		mav.addObject("list", tbInviteService.getList(invMap));
		mav.addObject("ehi_name", ehi_name);
		return mav;
	}
	@RequestMapping("unit/detailInvite.do")	
	public ModelAndView detailInvite(HttpServletRequest request,String inv_id){
		ModelAndView mav=new ModelAndView("./invite/detail.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("inv_id", inv_id);
		mav.addObject("exhinfo", tbInviteService.getOne(map));
		return mav;
	}
	/**
	 * 审核
	 * @param request
	 * @param inv_id
	 * @param exh_id
	 * @param state
	 * @return
	 */
	@RequestMapping("unit/verify.do")	
	public ModelAndView verify(HttpServletRequest request,String inv_id,String exh_id,String state){
		ModelAndView mav=new ModelAndView("redirect:/unit/inviteList.do");
		if(state.equals("2")){//加入展会
			Map<String,Object> map1=new HashMap<String,Object>();
			map1.put("id", Util.getPrimaryKey());
			map1.put("unit_id", request.getSession().getAttribute("unit_id"));
			map1.put("exh_id", exh_id);
			tbExhUnitService.add(map1);
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("inv_id", inv_id);
		map.put("inv_state", state);
		tbInviteService.update(map);
		return mav;
	}
}
